using Microsoft.Extensions.Configuration;
using Serilog;
using XjjXmm.Infrastructure.ToolKit;

namespace XjjXmm.Infrastructure.Configuration;

public static class ConfigSetUp
{
    /// <summary>
    /// 加载配置文件
    /// </summary>
    /// <param name="fileName">文件名称</param>
    /// <param name="environmentName">环境名称</param>
    /// <param name="reloadOnChange">自动更新</param>
    /// <returns></returns>
    public static IConfigurationBuilder Scan(this IConfigurationBuilder configurationBuilder, string environmentName = "", bool reloadOnChange = false)
    {
        var filePath = AppKit.GetCurrentDirectory();
        configurationBuilder.SetBasePath(filePath);

/*config = new ConfigurationBuilder()
    .SetBasePath(filePath);*/


var configFilePath = Path.Combine(filePath, "configs");
        if (Directory.Exists(configFilePath))
        {
				
            foreach (var enumerateFile in Directory.EnumerateFiles(configFilePath, "*.json"))
            {
					

                var file = new FileInfo(enumerateFile);

                configurationBuilder.AddJsonFile("configs/" + file.Name, true, reloadOnChange);
					
            }
        }
        else
        {
            Directory.CreateDirectory(configFilePath);
        }

        configurationBuilder.AddJsonFile("appsettings.json", true, reloadOnChange);
        configurationBuilder.AddJsonFile($"appsettings.{environmentName}.json", true, reloadOnChange);
        return configurationBuilder;
			
    }
}